/**
TODO: SessionID or RoomID
Pass the session id to server..
**/
define([], function () {

    return {
    	config : {
    		strokeWidth: 1, 
    		strokeColor: 'black',
    		strokeStyle:[],
    		fillMode : false,
    		fillColor : 'black',
    		roundedMode : false,
    		cornerRadius : 10,
    		arrowMode : false,
			alpha : 100,
    		
    		setEditColor : function(color){
				App.kinetic.shape.config.strokeColor = color;
    		},
			setEditFillColor : function(color){
				App.kinetic.shape.config.fillColor = color;
    		},
						
    		setStrokeWidth : function(strokeWidth){
    			console.log("Kinetic Shape : Set stroke width to " + strokeWidth);
    			App.kinetic.shape.config.strokeWidth = strokeWidth;
    		},
    		
    		setStrokeStyle : function(style){
    			console.log("Kinetic Stage : Set line style to " + style);
				if(style=='dashed')
					App.kinetic.shape.config.strokeStyle = [5*App.kinetic.shape.config.strokeWidth,5*App.kinetic.shape.config.strokeWidth];
				if(style=='dotted')
					App.kinetic.shape.config.strokeStyle = [2,2*App.kinetic.shape.config.strokeWidth];
				if(style=='normal')
					App.kinetic.shape.config.strokeStyle = [];
			},
			
			setActiveFillMode : function(mode){
				App.kinetic.shape.config.fillMode = mode;
			},
			
			setActiveRoundedMode : function(mode){
				App.kinetic.shape.config.roundedMode = mode;
			},
			
			setActiveArrowMode : function(mode){
				App.kinetic.shape.config.arrowMode = mode;
			}
			
    	},
    
		createRect : function(x,y,width,height){
			console.log("Kinetic Shape : Create Box");
			var box = new Kinetic.Rect({
				x: 0,
				y: 0,
				width: width,
				height: height,
				stroke: App.kinetic.shape.config.strokeColor,
				strokeWidth: App.kinetic.shape.config.strokeWidth
			});
			
			return box;
		},
		
		initRectForCreation : function(point){
			var fill = App.kinetic.shape.config.fillColor;
			var radius = 0;
			if(!App.kinetic.shape.config.fillMode)
				fill = "";
				
			if(App.kinetic.shape.config.roundedMode)
				radius = App.kinetic.shape.config.cornerRadius;
			
			rect = new Kinetic.Rect({
				x: point.x,
				y: point.y,
				stroke : App.kinetic.shape.config.strokeColor,
				fill : fill,
				strokeWidth: App.kinetic.shape.config.strokeWidth,
				cornerRadius : radius,
				dashArray : App.kinetic.shape.config.strokeStyle,
				opacity : App.kinetic.shape.config.alpha/100.0				
			  });
			return rect;
		},
		
		initCircleForCreation : function(point){
			var fill = App.kinetic.shape.config.fillColor;
			if(!App.kinetic.shape.config.fillMode)
				fill = "";
				
			circle = new Kinetic.Ellipse({
				x: point.x,
				y: point.y,
				radius: {
				  x: 1,
				  y: 1
				},
				fill: fill,
				stroke: App.kinetic.shape.config.strokeColor,
				strokeWidth: App.kinetic.shape.config.strokeWidth,
				dashArray : App.kinetic.shape.config.strokeStyle,
				opacity : App.kinetic.shape.config.alpha/100.0
			  });
			  
			return circle;
		},
		
		initPencilForCreation : function(point){
			pencil = new Kinetic.Line({
				points: [point.x,point.y,point.x,point.y],
				stroke: App.kinetic.shape.config.strokeColor,
				strokeWidth: App.kinetic.shape.config.strokeWidth,
				lineCap: 'round',
				lineJoin: 'round',
				dashArray : App.kinetic.shape.config.strokeStyle,
				opacity : App.kinetic.shape.config.alpha/100.0
			  });
			return pencil;
		},
		
		initLineForCreation : function(point){
			var points;
			var tox = point.x;
			var toy = point.y;
			var headlen = 20;
			var angle = Math.atan2(toy-toy,tox-tox);
			if(App.kinetic.shape.config.arrowMode){
				points = [{x:point.x,y:point.y},{x:point.x,y:point.y},{x:tox-headlen*Math.cos(angle-Math.PI/6),y:toy-headlen*Math.sin(angle-Math.PI/6)},{x:tox, y:toy}, {x:tox-headlen*Math.cos(angle+Math.PI/6),y:toy-headlen*Math.sin(angle+Math.PI/6)}];
			}else{
				points = [{x:point.x,y:point.y},{x:point.x,y:point.y}];
			}
			line = new Kinetic.Line({
				points: points,
				stroke: App.kinetic.shape.config.strokeColor,
				strokeWidth: App.kinetic.shape.config.strokeWidth,
				lineCap: 'round',
				lineJoin: 'round',
				dashArray : App.kinetic.shape.config.strokeStyle,
				opacity : App.kinetic.shape.config.alpha/100.0
			  });
			
			 return line;
		},
		
		settingRectOnCreation : function(anchorPoint,point,rect){
			var p = rect.attrs,
			w = point.x - p.x,
			h = point.y - p.y;
			rect.setX(anchorPoint.x);
			rect.setY(anchorPoint.y);			
			rect.setWidth(w);
			rect.setHeight(h);
		},
		
		settingCircleOnCreation : function(anchorPoint,point,circle){
			var p = circle.attrs,
				w = Math.abs(point.x - p.x),
				h = Math.abs(point.y - p.y);
				
			circle.setX(anchorPoint.x+w/2);
			circle.setY(anchorPoint.y+h/2);
			circle.setRadius([w,h]);
		},
		
		settingPencilOnCreation : function(point,pencil){
			/*var array = pencil.attrs.points;
			array.push(point);
			pencil.setPoints(array);
			*/
			pencil.attrs.points.push(point);
		},
		
		settingLineOnCreation : function(point,line){
			if(!App.kinetic.shape.config.arrowMode){
				line.getPoints()[1].x = point.x;
				line.getPoints()[1].y = point.y;
			}else{
				var tox = point.x;
				var toy = point.y;
				var headlen = 20;
				var angle = Math.atan2(toy-line.getPoints()[0].y,tox-line.getPoints()[0].x);
				line.getPoints()[1].x = point.x;
				line.getPoints()[1].y = point.y;
				line.getPoints()[2].x = tox-headlen*Math.cos(angle-Math.PI/6);
				line.getPoints()[2].y = toy-headlen*Math.sin(angle-Math.PI/6);
				line.getPoints()[3].x = tox;
				line.getPoints()[3].y = toy;
				line.getPoints()[4].x = tox-headlen*Math.cos(angle+Math.PI/6);
				line.getPoints()[4].y = toy-headlen*Math.sin(angle+Math.PI/6);
			}
		}
		
	}
});